package com.yenlien.traceyun.usercenter.service;

import com.yenlien.traceyun.usercenter.dao.CompanyCustomAppVersionDao;
import com.yenlien.traceyun.usercenter.dao.CustomAppDao;
import com.yenlien.traceyun.usercenter.dao.CustomAppVersionDao;
import com.yenlien.traceyun.usercenter.dao.ResourceCustomAppVersionDao;
import com.yenlien.traceyun.usercenter.entity.CompanyCustomAppVersion;
import com.yenlien.traceyun.usercenter.entity.CustomApp;
import com.yenlien.traceyun.usercenter.entity.CustomAppVersion;
import com.yenlien.traceyun.usercenter.entity.ResourceCustomAppVersion;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-29
 * @description:
 **/

@Service
public class AppService {

    @Autowired
    private CustomAppDao customAppDao;

    @Autowired
    private FileService fileService;

    @Autowired
    private CustomAppVersionDao customAppVersionDao;

    @Autowired
    private ResourceCustomAppVersionDao resourceCustomAppVersionDao;
    @Autowired
    private CompanyCustomAppVersionDao companyCustomAppVersionDao;

    public Page<CustomApp> findByList(final CustomApp customApp) {
        Pageable pageable = PageRequest.of(customApp.getPage(), customApp.getPageSize());
        return customAppDao.findAll(new Specification<CustomApp>() {
            @Override
            public Predicate toPredicate(Root<CustomApp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
    }

    /**
     * 创建自定义app
     *
     * @param customApp
     * @param files
     * @return
     * @throws Exception
     */
    public CustomApp createCustomApp(CustomApp customApp, List<MultipartFile> files) throws Exception {
        fileService.upload(null, customApp, files);
        return createCustomAppInfo(customApp);
    }

    public CustomApp createCustomAppInfo(CustomApp customApp) throws Exception {
        customApp.setCreateTime(new Date());
        // name 不能重复
        List<CustomApp> byName = customAppDao.findByName(customApp.getName());
        if (byName == null || byName.size() == 0) {
            return customAppDao.save(customApp);
        }
        throw new BusinessException("名字不能重复");
    }




    public CustomApp createCustomApp2(CustomApp customApp) throws Exception {
        return createCustomAppInfo(customApp);
    }



    /**
     * 更新自定义app
     *
     * @param customApp
     * @param files
     * @return
     * @throws Exception
     */
    public CustomApp updateCustomApp(CustomApp customApp, List<MultipartFile> files) throws Exception {
        fileService.upload(customAppDao.findById(customApp.getId()).get(), customApp, files);
        return customAppDao.save(customApp);
    }

    /**
     * 创建自定义app版本
     *
     * @param customAppVersion
     * @param resourceIds
     * @return
     */
    @Transactional
    public CustomAppVersion createCustomAppVersion(CustomAppVersion customAppVersion, List<String> resourceIds) {
        customAppVersionDao.save(customAppVersion);
        deleteResourceCustomAppVersions(customAppVersion.getId());
        List<ResourceCustomAppVersion> resourceCustomAppVersions = new ArrayList<>();
        if (resourceIds != null && resourceIds.size() > 0) {
            for (String resource : resourceIds) {
                ResourceCustomAppVersion resourceCustomAppVersion = new ResourceCustomAppVersion();
                resourceCustomAppVersion.setAppId(customAppVersion.getAppId());
                resourceCustomAppVersion.setCustomAppId(customAppVersion.getCustomAppId());
                resourceCustomAppVersion.setCustomAppVersionId(customAppVersion.getId());
                resourceCustomAppVersion.setResourceId(resource);
                resourceCustomAppVersions.add(resourceCustomAppVersion);
            }
            resourceCustomAppVersionDao.saveAll(resourceCustomAppVersions);
        }

        return customAppVersion;
    }

    private void deleteResourceCustomAppVersions(String customAppVersionId) {
        List<ResourceCustomAppVersion> rs = resourceCustomAppVersionDao.findByCustomAppVersionId(customAppVersionId);
        if (rs != null && rs.size() > 0) {
            resourceCustomAppVersionDao.deleteAll(rs);
        }
    }


    public CustomAppVersion findCustomAppVersionId(String id) {
        CustomAppVersion customAppVersion = customAppVersionDao.findById(id);
        customAppVersion.setRes(resourceCustomAppVersionDao.findByCustomAppVersionId(id));
        return customAppVersion;
    }

    public Set<String> findByModelIds(String companyId, String appId) {
        List<CompanyCustomAppVersion> companyCustomAppVersion = companyCustomAppVersionDao.findByCompanyIdAndAppId(companyId, appId);
        Set<String> modelIds = new HashSet<>();
        if (companyCustomAppVersion != null && companyCustomAppVersion.size() > 0) {
            for (CompanyCustomAppVersion vs : companyCustomAppVersion) {
                CustomAppVersion customAppVersion = customAppVersionDao.findById(vs.getCustomAppVersionId());
                modelIds.addAll(Arrays.asList(customAppVersion.getModelIds().split(",")));
            }
        }
        return modelIds;
    }


}
